package com.epsoft.odin7cj.flow.example;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.util.List;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

/**
 * 参数及附件
 * @author Administrator
 *
 */
public class C06TaskParam {

	public static void main1(String[] args) throws Exception {
		
		// 流程引擎
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		
	    RepositoryService rs = engine.getRepositoryService();
	    
	    TaskService ts = engine.getTaskService();
	    
	    RuntimeService rus = engine.getRuntimeService();
	    
	    Deployment deploy = rs.createDeployment().addClasspathResource("bpmn/c06-task-param.bpmn").deploy();
	    
	    ProcessDefinition pd = rs.createProcessDefinitionQuery().deploymentId(deploy.getId()).singleResult();
	    
	    ProcessInstance ps = rus.startProcessInstanceById(pd.getId());
	    
	    Task task = ts.createTaskQuery().processInstanceId(ps.getId()).singleResult();
	    
	    // 设置参数
		TestUser testUser = new TestUser();
		testUser.setId("23");
		testUser.setLoginname("bruce");
		testUser.setPassword("hu");
	    ts.setVariable(task.getId(), "testUser", testUser);
	    
	    // 设置附件
	    InputStream inputStream = new FileInputStream(new File("E:\\project_oxygen\\ufdm\\ufdm-flow\\src\\main\\resources\\bpmn\\test.jpg"));
	    ts.createAttachment("1", task.getId(), ps.getId(), "身份证明", "证明身份用", inputStream);
	    
	    
	    System.out.println("\n 当前任务："+task.getName());
	    
	    
	    // 获取存储的对象
	    TestUser tu = ts.getVariable(task.getId(), "testUser", TestUser.class);
	    
	    System.out.println("获取参数:"+tu);
	    
	    // 完成当前节点
	    ts.complete(task.getId());
	    
        task = ts.createTaskQuery().processInstanceId(ps.getId()).singleResult();
	    
        System.out.println("\n 当前任务："+task.getName());
	    
	    tu = ts.getVariable(task.getId(), "testUser", TestUser.class);
	    
	    System.out.println("获取参数:"+tu);
	}
	
}


class TestUser implements Serializable {
	
	private static final long serialVersionUID = 8379448020418356735L;
	
	private String id;
	private String loginname;
	private String password;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getLoginname() {
		return loginname;
	}
	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Override
	public String toString() {
		return "ParamObject [id=" + id + ", loginname=" + loginname + ", password=" + password + "]";
	}
	
	
}
